package edu.uts.reekie.designer.edit;

import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.ConnectionEndpointEditPolicy;

import edu.uts.reekie.designer.actions.ClearConnectorLabelAction;
import edu.uts.reekie.designer.actions.ClearSourceToTargetConnectorLabelAction;
import edu.uts.reekie.designer.actions.ClearTargetToSourceConnectorLabelAction;
import edu.uts.reekie.designer.actions.SetConnectorLabelAction;
import edu.uts.reekie.designer.actions.SetSourceToTargetConnectorLabelAction;
import edu.uts.reekie.designer.actions.SetTargetToSourceConnectorLabelAction;
import edu.uts.reekie.designer.model.AbstractConnector;
import edu.uts.reekie.designer.model.commands.SetConnectorLabelCommand;

public class AbstractConnectorEndpointEditPolicy extends
		ConnectionEndpointEditPolicy {

	public static final String NEW_LABEL = "new label";
	public static final String LABEL_TYPE = "label type";
	
	public AbstractConnectorEndpointEditPolicy() {
		super();
	}
	
	
	public Command getCommand(Request request) {
	
		Object rtype = request.getType();

		String val = (String)request.getExtendedData().get(NEW_LABEL);
		
		
		if (rtype.equals(SetConnectorLabelAction.NORMAL)) {
			SetConnectorLabelCommand c = new SetConnectorLabelCommand(val,AbstractConnector.LABEL_BI_DIRECTIONAL);
			c.setConnector((AbstractConnector)getHost().getModel());
			return c;
		} else if(rtype.equals(SetTargetToSourceConnectorLabelAction.TARGET_TO_SOURCE)) {
			SetConnectorLabelCommand c = new SetConnectorLabelCommand(val,AbstractConnector.LABEL_TARGET_TO_SOURCE);
			c.setConnector((AbstractConnector)getHost().getModel());
			return c;
			
		} else if(rtype.equals(SetSourceToTargetConnectorLabelAction.SOURCE_TO_TARGET)) {
			SetConnectorLabelCommand c = new SetConnectorLabelCommand(val,AbstractConnector.LABEL_SOURCE_TO_TARGET);
			c.setConnector((AbstractConnector)getHost().getModel());
			return c;
		}
		
		// TODO: support clear commands
		if (rtype.equals(ClearConnectorLabelAction.NORMAL)) {
			SetConnectorLabelCommand c = new SetConnectorLabelCommand(val,AbstractConnector.LABEL_BI_DIRECTIONAL);
			c.setConnector((AbstractConnector)getHost().getModel());
			return c;
		} else if(rtype.equals(ClearTargetToSourceConnectorLabelAction.TARGET_TO_SOURCE)) {
			SetConnectorLabelCommand c = new SetConnectorLabelCommand(val,AbstractConnector.LABEL_TARGET_TO_SOURCE);
			c.setConnector((AbstractConnector)getHost().getModel());
			return c;
			
		} else if(rtype.equals(ClearSourceToTargetConnectorLabelAction.SOURCE_TO_TARGET)) {
			SetConnectorLabelCommand c = new SetConnectorLabelCommand(val,AbstractConnector.LABEL_SOURCE_TO_TARGET);
			c.setConnector((AbstractConnector)getHost().getModel());
			return c;
		}
		
		return  null;
		
	}
}

